/**
 * <p>Copyright (c) 2011 深圳市鹏途信息技术有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	东莞交通工程质量监督综合业务系统        </p>
 * <p>创建者   :	Administrator 
 * 
 * <p>描   述  :   DetectionorgAction.java for com.pengtu.web.project.org    </p>
 * 
 * <p>最后修改 : $: 2011-8-26-下午02:40:40 v 1.0.0	 Administrator   $     </p>
 * 
*/

package com.pengtu.web.project.org;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.project.DetectOrganization;
import com.pengtu.service.project.SignOrganizationManager;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

/**
 * 
 * DetectionorgAction
 * 
 * 2011-8-26 下午02:40:40
 * 
 * @version 1.0.0
 * 
 */
@Namespace("/project/org")
@Results({ @Result(name = BaseActionSupport.RELOAD, type = "redirect", location = "organization.action"),
	@Result(name = "orgsuc", location = "/orgsuccess.jsp?orgId=${id}&orgName=${orgName}", type = "redirect")})
@Result(name = BaseActionSupport.VIEW, location = "${ctx}/project/sign/org/organization!signlist.action?typeId=${typeId}", type = "redirect")
public class DetectionorgAction  extends BaseActionSupport<DetectOrganization>{

	/**
	 * serialVersionUID:TODO
	 *
	 * @since 1.0.0
	 */
	
	private static final long serialVersionUID = 1L;
	private SignOrganizationManager signOrgManager;
	private String id;
	private DetectOrganization detectOrganization;
	private Page<DetectOrganization> page = new Page<DetectOrganization>(10);// 每页5条记录
	private String typeId;

	@Override
	public DetectOrganization getModel() {
		return detectOrganization;
	}

	@Override
	public String list() throws Exception {
		QueryFilter queryfilters = QueryFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		// 设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.DESC);
		}
		page = signOrgManager.findPageDetection(page, queryfilters);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		signOrgManager.saveDetectOrg(detectOrganization);
		String mes = "";
		if(StringUtils.isNotEmpty(id)){
			mes = "修改";
		}else{
			mes = "新增";
		}
		addActionMessage(mes+"检测单位");
		if(StringUtils.isNotEmpty(typeId)){
			Struts2Utils.getSession().setAttribute("orgId",  "");
			Struts2Utils.getSession().setAttribute("orgName",  "");
			Struts2Utils.getSession().setAttribute("orgId",  detectOrganization.getId());
			Struts2Utils.getSession().setAttribute("orgName",  detectOrganization.getOrgName());
			return "orgsuc";
		}
		return  input();
	}

	@Override
	public String delete() throws Exception {
		signOrgManager.deleteDetectOrg(id);
		return null;
	}

	
	/**
	 * 验证机构代码是否重复
	 */
	public void checkOrgNum() {
		// TODO Auto-generated method stub
		String orgNum = Struts2Utils.getParameter("orgNum");
		String oldOrgNum = Struts2Utils.getParameter("oldOrgNum");
		if (signOrgManager.isOrgNumUnique(orgNum, oldOrgNum)) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}
	}

	/**
	 * 验证单位名是否重复
	 */
	public void checkOrgName() {
		// TODO Auto-generated method stub
		String orgName = Struts2Utils.getParameter("orgName");
		String oldOrgName = Struts2Utils.getParameter("oldOrgName");
		if (signOrgManager.isOrgNameUnique(orgName, oldOrgName)) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}
	}

	@Override
	protected void prepareModel() throws Exception {
		
		if (StringUtils.isNotEmpty(id)) {
			detectOrganization = signOrgManager.getDetect("id", id);
		} else {
			detectOrganization = new DetectOrganization();
		}
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	
	@Autowired
	public void setSignOrgManager(SignOrganizationManager signOrgManager) {
		this.signOrgManager = signOrgManager;
	}

	public Page<DetectOrganization> getPage() {
		return page;
	}

	public void setPage(Page<DetectOrganization> page) { 
		this.page = page;
	}

	public String getTypeId() {
		return typeId;
	}

	public void setTypeId(String typeId) {
		this.typeId = typeId;
	}

}
